
import axios from 'axios'
import { ElMessage } from 'element-plus' // 示例用 ElementPlus 提示

// 创建实例
const service = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL, // 从环境变量读取
    timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(config => {
    const token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
}, error => {
    return Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(
    response => response.data,
    error => {
        ElMessage.error(error.response?.data?.message || '请求异常')
        return Promise.reject(error)
    }
)

export default service
